import com.zwc.kotlin.coroutines.delay
import kotlin.coroutines.*


fun main() {
    //callLaunchCoroutine()
    /*runSuspend(block = suspend {
        1
    })*/
  /*  test(FunctionBean(), block = { b, c ->
        this.add(b, c).toString()

    })*/
    val aa = suspend {
      val aaaaa=  suspendCoroutine<String> {
            2222
          "saaaaa"
          it.resume("sssss")
        }
        println(aaaaa)
        delay(2000)
        111
    }.createCoroutine(object : Continuation<Int> {
        override val context: CoroutineContext
            get() = EmptyCoroutineContext

        override fun resumeWith(result: Result<Int>) {
            println("Coroutine End: $result")
        }

    })
    aa.resumeWith(Result.success(Unit))

}

fun <R, T> launchCoroutine(receiver: R, block: suspend R.() -> T) {
    val aa = block.startCoroutine(receiver, object : Continuation<T> {
        override val context: CoroutineContext
            get() = EmptyCoroutineContext

        override fun resumeWith(result: Result<T>) {
            println("Coroutine End: $result")
        }

    })
    println(aa)
}

fun test(functionBean: FunctionBean, block: FunctionBean.(b: Int, c: Int) -> String) {
    println(block(functionBean, 15, 2))
}

fun test2(functionBean: FunctionBean, block: FunctionBean.() -> String) {
    block(functionBean)
}

class FunctionBean {
    fun add(a1: Int, a2: Int): Int {
        return a1 + a2
    }
}

fun foo(quex: (a1: Int, a2: Int) -> Int) {
    println(quex(1, 3))

}

class ProducerScope<T> {
    suspend fun produce(value: T) {
        println(value)
    }
}

@RestrictsSuspension
class RestrictProducerScope<T> {
    suspend fun produce(value: T) {

    }
}

fun callLaunchCoroutine() {
    launchCoroutine(RestrictProducerScope<Int>()) {
        println("In coroutine.")
        produce(1024)
        produce(2048)
        return@launchCoroutine 1221
    }
    launchCoroutine(Int){

    }
}

internal fun runSuspend(block: suspend () -> Int) {
    val run = RunSuspend()
    block.startCoroutine(run)
}

class RunSuspend : Continuation<Int> {
    override val context = EmptyCoroutineContext
    override fun resumeWith(result: Result<Int>) {
        println("Coroutine End: $result")
    }
suspend fun notSuspend()= suspendCoroutine<Int> {continuation ->
    continuation.resume(1)
}
}